/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.funda.xmppserver.handlers.iqhandlers;

import com.funda.xmppserver.ComponentParser;
import com.funda.xmppserver.XMPPClient;
import com.funda.xmppserver.mysql.DBManager;
import com.funda.xmppserver.roster.Roster;
import com.funda.xmppserver.writer.XMPPPacketWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author kurinchi
 */
public class IQQueryHandler implements ComponentParser {

    XMPPPacketWriter packetwriter;
    XMPPClient client;
    Connection conn;
    Statement smt;
    ResultSet rs;
    Boolean valid = true;
    String id;
    String type;
    String clientjid;

    public IQQueryHandler(XMPPPacketWriter packetwriter, XMPPClient client, String id, String type) {
        this.packetwriter = packetwriter;
        this.client = client;
        this.id = id;
        this.type = type;
        clientjid = client.getJid().toString();
    }

    public void parseElement(XMLStreamReader xmlReader) throws XMLStreamException {
        String qnamespace = xmlReader.getNamespaceURI(0);

        if (qnamespace.equalsIgnoreCase("http://jabber.org/protocol/disco#items")) {
            packetwriter.senddiscoResponse(id, clientjid);
        }
        if (qnamespace.equalsIgnoreCase("jabber:iq:agents")) {
            packetwriter.sendagentsResponse(id, clientjid);
        }
        if (qnamespace.equalsIgnoreCase("jabber:iq:register")) {
            String username = "";
            String password = "";
            String email = "";
            String name = "";
            String tmp = "";

            boolean flg = true;
            while (xmlReader.hasNext()) {

                xmlReader.next();
                int event = xmlReader.getEventType();

                if (event == XMLEvent.START_ELEMENT) {
                    tmp = xmlReader.getLocalName();
                    if (tmp.equalsIgnoreCase("username")) {
                        username = xmlReader.getElementText();
                        flg = false;
                    }
                    if (tmp.equalsIgnoreCase("password")) {
                        password = xmlReader.getElementText();
                    }
                    if (tmp.equalsIgnoreCase("email")) {
                        email = xmlReader.getElementText();
                    }
                    if (tmp.equalsIgnoreCase("name")) {
                        name = xmlReader.getElementText();
                    }
                }
                if (event == XMLEvent.END_ELEMENT && xmlReader.getLocalName().equalsIgnoreCase("query")) {
                    break;
                }
            }
            if (flg) {
                packetwriter.sendinitregisterResponse(id);
            } else {
                if (DBManager.updateUserdetails(username, password, email, name)) {
                    packetwriter.sendregisterResponse(id);
                } else {
                    packetwriter.sendregisterconflictResponse(id, username, password);
                }
            }
        }
        if (qnamespace.equalsIgnoreCase("jabber:iq:roster")) {

            //check for subscription request
            while (xmlReader.hasNext()) {
                xmlReader.next();
                int event = xmlReader.getEventType();

                if (event == XMLEvent.START_ELEMENT && xmlReader.getLocalName().equalsIgnoreCase("item")) {
                    if (xmlReader.getAttributeLocalName(0).equalsIgnoreCase("jid")) {
                        String jid = xmlReader.getAttributeValue(0);
                        client.getRoster().getRosteritems().put(jid, "none");


                        //DBManager.updateRoster(client.getUsername(), "none", jid.substring(0, jid.indexOf("@")));
                        packetwriter.sendSubscriptionrequsetResponse(jid, id);
                        return;

                    }
                }
                if (event == xmlReader.END_ELEMENT) {
                    break;
                }
            }

            Roster roster = client.getRoster();

            System.out.println(roster.getItems());
            packetwriter.sendrosterResponse(id, clientjid, roster.getItems());
        }

    }
}
